package zt

import (
	"encoding/json"
	"reflect"
)

func Struct2Map(v interface{}) map[string]interface{} {
	m := make(map[string]interface{})
	j, _ := json.Marshal(v)
	json.Unmarshal(j, &m)
	return m
}

func Struct2MapByReflect(v interface{}) map[string]interface{} {
	m := make(map[string]interface{})
	elem := reflect.ValueOf(v).Elem()
	relType := elem.Type()
	for i := 0; i < relType.NumField(); i++ {
		m[relType.Field(i).Name] = elem.Field(i).Interface()
	}
	return m
}
